package se.webbzon.boltzmann.game.sequence.condition;

import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.SequenceNode;
import se.webbzon.oschi01.worldobject.WorldObject;

public abstract class ConditionSequenceNode extends SequenceNode {

	// The left and right hand argument
	private final String lh, rh;
	
	// The true and false branches of this condition sequence node
	private SequenceNode t, f;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new condition node. **/
	public ConditionSequenceNode(Sequence sequence, String lh, String rh) {
		super(sequence);
		this.lh = lh;
		this.rh = rh;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns true if this condition node is true. **/
	public abstract boolean isTrue();
	
	/** Returns the left hand argument of this condition sequence node. **/
	public String getLeftHandArgument() {
		return lh;
	}
	
	/** Returns the right hand argument of this condition sequence node. **/
	public String getRightHandArgument() {
		return rh;
	}
	
	/** Sets the true-branch for this condition sequence node. **/
	public void setTrueBranch(SequenceNode branch) {
		t = branch;
	}
	
	/** Sets the false-branch for this condition sequence node. **/
	public void setFalseBranch(SequenceNode branch) {
		f = branch;
	}
	
	@Override public int branches() {
		return 2;
	}

	@Override public SequenceNode getBranch(int n) {
		if (n == 0)
			return t;
		else
			return f;
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	@Override protected void onBegin(WorldObject binding) {
		final int branch;
		if (isTrue())
			branch = 0;
		else
			branch = 1;
		getSequence().next(branch);
	}

	@Override protected void onEnd(WorldObject binding) {};

}
